/**
 * Copyright (c) 2014- 2015 YCY Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.jkwzms.common.plugin.web.controller;

import java.io.Serializable;

import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jkwzms.common.entity.BaseEntity;
import com.jkwzms.common.entity.search.Searchable;
import com.jkwzms.common.pagination.Pagination;
import com.jkwzms.common.plugin.entity.Movable;
import com.jkwzms.common.plugin.serivce.BaseMovableService;
import com.jkwzms.common.utils.MessageUtils;
import com.jkwzms.common.web.bind.annotation.PageableDefaults;
import com.jkwzms.common.web.controller.BaseCRUDController;
import com.jkwzms.common.web.validate.AjaxResponse;
import com.jkwzms.system.user.entity.User;

/**
 * <p>
 * User: Jack Liang
 * <p>
 * Date: 13-2-22 下午4:15
 * <p>
 * Version:1.0.0
 */
public abstract class BaseMovableController<M extends BaseEntity & Movable, ID extends Serializable> extends BaseCRUDController<M, ID> {

    private BaseMovableService movableService;

    @Override
    public void afterPropertiesSet() {
        super.afterPropertiesSet();

        Assert.isTrue(BaseMovableService.class.isAssignableFrom(baseService.getClass()),
                      "baseService must be BaseMovableService subclass");

        this.movableService = (BaseMovableService<M, ID>) this.baseService;

    }

    // @RequestMapping(method = RequestMethod.GET)
    // @PageableDefaults(value = 10, sort = "weight=desc")
    // @Override
    // public Pagination<M> list(Searchable searchable, Model model) {
    // return null;
    // }

    @RequestMapping(value = "{fromId}/{toId}/up")
    @ResponseBody
    public AjaxResponse up(@PathVariable("fromId") Long fromId, @PathVariable("toId") Long toId) {

        if (this.permissionList != null) {
            this.permissionList.assertHasEditPermission();
        }

        AjaxResponse ajaxResponse = new AjaxResponse("移动位置成功");
        try {
            movableService.up(fromId, toId);
        } catch (IllegalStateException e) {
            ajaxResponse.setSuccess(Boolean.FALSE);
            ajaxResponse.setMessage(MessageUtils.message("move.not.enough"));
        }
        return ajaxResponse;
    }

    @RequestMapping(value = "{fromId}/{toId}/down")
    @ResponseBody
    public AjaxResponse down(@PathVariable("fromId") Long fromId, @PathVariable("toId") Long toId) {

        if (this.permissionList != null) {
            this.permissionList.assertHasEditPermission();
        }

        AjaxResponse ajaxResponse = new AjaxResponse("移动位置成功");
        try {
            movableService.down(fromId, toId);
        } catch (IllegalStateException e) {
            ajaxResponse.setSuccess(Boolean.FALSE);
            ajaxResponse.setMessage(MessageUtils.message("move.not.enough"));
        }
        return ajaxResponse;
    }

    @RequestMapping(value = "reweight")
    @ResponseBody
    public AjaxResponse reweight() {

        if (this.permissionList != null) {
            this.permissionList.assertHasEditPermission();
        }

        AjaxResponse ajaxResponse = new AjaxResponse("优化权重成功！");
        try {
            movableService.reweight();
        } catch (IllegalStateException e) {
            ajaxResponse.setSuccess(Boolean.FALSE);
            ajaxResponse.setMessage("优化权重失败了！");
        }
        return ajaxResponse;
    }

}
